package petmall.com.testssoclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * @author liang zai
 * @date 2022/4/12 17:16
 * @description
 */
@Controller
public class HelloController {

    @Value("${sso.host}")
    private String LoginUser;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @GetMapping("/emplayess")
    public String emplayess(Model model, HttpSession session,@RequestParam(value = "token",required = false) String token){
        if (StringUtils.hasText(token)){
            //认为去其他页面登录过，带回来了一个token
            session.setAttribute("loginUser","zhangsan");
        }
        Object toLogin = session.getAttribute("loginUser");
        if (toLogin == null){
            //没有登录 跳转到登录页面
            return "redirect:" + LoginUser + "?redirect_url=http://client1.com:8083/emplayess";
        }else {
            //登录了
            ArrayList<String> objects = new ArrayList<>();
            objects.add("张三");
            objects.add("李四");
            Model emps = model.addAttribute("emps", objects);
            return "list";
        }
    }
}
